/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sismol.embarcado;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import sismol.cartography.Posicao;

/**
 *
 * @author Rafael
 */
public class ServerConnector {

    private BufferedWriter outFile;
    private FileWriter fstreamOut;
    private Double distancia1 = 0.0d;

    public ServerConnector() {
        try {
            int i = 0;
            while (true) {
                File f = new File("out" + i + ".txt");
                if (f.exists()) {
                    i++;
                } else {
                    break;
                }
            }
            this.fstreamOut = new FileWriter("out" + i + ".txt");
            this.outFile = new BufferedWriter(fstreamOut);
            this.fstreamOut = new FileWriter("out" + i + ".txt");
        } catch (IOException e) {
            throw new RuntimeException("Impossível criar outFile: " + e.getMessage());
        }


    }

    public void sendData(Posicao posicao, Double distancia) {
        try{
        System.out.println("  Latitude: " + posicao.getPosicao().getLatitude());
        outFile.write("  Latitude: " + posicao.getPosicao().getLatitude());
        outFile.newLine();

        System.out.println("  Longitude: " + posicao.getPosicao().getLongitude());
        outFile.write("  Longitude: " + posicao.getPosicao().getLongitude());
        outFile.newLine();

        System.out.println("  Horario: " + posicao.getHorario());
        outFile.write("  Horario: " + posicao.getHorario());
        outFile.newLine();

        System.out.println("  Velocidade: " + posicao.getVelocidadeMedia());
        outFile.write("  Velocidade: " + posicao.getVelocidadeMedia());
        outFile.newLine();

        distancia1 = distancia1 + posicao.getVelocidadeMedia() / 3.6;
        System.out.println("  Distancia percorrida: " + distancia1);
        outFile.write("  Distancia percorrida: " + distancia1);
        outFile.newLine();

        System.out.println("  Distancia percorrida (Haversine): " + distancia);
        outFile.write("  Distancia percorrida (Haversine): " + distancia);
        outFile.newLine();
        outFile.flush();
        }
        catch(IOException e){
            System.out.println("Impossível escrever em out: " + e.getMessage());
        }
    }
}
